load("js/luaclient.js");
load('js/v6/abc/abc_common.js');
load("js/v6/abc/abc_lua.js");
/**
 * 请求URL
 * @type {{}}
 */
var urls = {
    // 登录页请求地址
    REQ_LOGIN_HTML_URL : "https://perbank.abchina.com/EbankSite/startup.do",
    // 经典版首页请求地址
    REQ_CLASSIC_INDEX_URL : "https://perbank.abchina.com/EbankSite/redirectToOldLogin.do",
    // 采集用户基本信息，卡类表信息
    REQ_BASIC_INFO_URL:"https://perbank.abchina.com/SelfBank/netBank/zh_CN/AcctQryIndexInitAct.ebf",
    // 时尚版首页地址
    REQ_LOGIN_FASHION_INDEX_HTML : "https://perbank.abchina.com/EbankSite/index.do",
    // 经典版跳转时尚版后的URL
    REQ_CLASSIC_JUMP_FASHION_URL : "https://perbank.abchina.com/EbankSite/autoLogin.do",
    // 查询借记卡信息地址
    REQ_ACCOUNT_INIT_URL : "https://perbank.abchina.com/EbankSite/MyAccountInitAct.do",
    // 查询借记卡存款信息地址
    REQ_ACCOUNT_BALANCE_URL : "https://perbank.abchina.com/EbankSite/AcctBalanceQryAct.do",
    // 查询借记卡存款信息地址
    REQ_ACCOUNT_DEPOSIT_URL : "https://perbank.abchina.com/EbankSite/SubAcctCurrQueryAct.do",
    // 请求账单数据地址
    REQ_SHOPPING_DOWNLOAD_URL :"https://perbank.abchina.com/EbankSite/AccountTradeDetailDownloadAct.do",
    //-----------------------信用卡---------------------
    // 请求信用卡基本信息地址
    REQ_CREDIT_CARD_INFO_URL:"https://perbank.abchina.com/EbankSite/CreditCardDetailAct.do",
    // 请求信用卡已出账单地址
    REQ_CREDIT_DONE_BILLS_URL:"https://perbank.abchina.com/EbankSite/ccBillQryAct.do",
    // 请求账单数据类表
    REQ_CREDIT_TOW_DONE_BILLS_URL:"https://perbank.abchina.com/EbankSite/ccBillQry2Act.do",
    // 请求账单数据明细请求
    REQ_CREDIT_DONE_BILL_INFO_URL:"https://perbank.abchina.com/EbankSite/ccBillQry1107Act.do",
    // 请求账单数据中的分期详细信息
    REQ_CREDIT_DONE_INSTALLMENT_INFO_URL:"https://perbank.abchina.com/EbankSite/ccBillQry1107Act.do",
    // 请求信用卡未出账单数据
    REQ_CREDIT_UNDONE_BILLS_URL:"https://perbank.abchina.com/EbankSite/ccBillwczdAct.do",
    /*-----------------------经典版，请求地址-----------------------------------*/
    // 经典版--请求借记卡类表基本信息
    REQ_SUTRA_BASIC_INFO_LIST_URL:"https://perbank.abchina.com/SelfBank/netBank/zh_CN/AcctQryIndexInitAct.ebf",
    // 经典版-卡信息请求地址
    REQ_SUTRA_BASIC_INFO_URL:"https://perbank.abchina.com/SelfBank/netBank/zh_CN/AcctInfQryInitAct.do",

}
/**
 * 采集请求数据
 * @type {{}}
 */
var collectReqData = {
    /**
     * --------------时尚版------------
     * 采集借记卡账号基本信息，卡列表信息
     */
    collectFashionCardList: function () {
        logger.info("ABCBank 时尚版，采集首页信息，taskKey=" + context.get(LUA_TASK_KEY) + "获取借记卡账户基本信息，开始");
        //时尚版首页地址
        var indexUrl = urls.REQ_LOGIN_FASHION_INDEX_HTML;
        //获取账户的基本信息
        var indexHtml = ajax_get(indexUrl);
        storeDataToFile(context, indexHtml, "index_fashionEdition.html");
        logger.info("ABCBank taskKey：{}，时尚版，采集首页信息，结束");
        context.put("fashion_basic_card_list",indexHtml)
    },
    /**
     * --------------时尚版-卡信息------------
     * 采集借记卡信息
     */
    collectFashionAccountCardInfo:function () {
        logger.info("collectFashionAccountCardInfo方法，采集时尚版-借记卡信息，开始");
        var accountCardInfoUrl = urls.REQ_ACCOUNT_INIT_URL;
        var deposit_cardInfoHtml = ajax_get(accountCardInfoUrl);
        storeDataToFile(context, deposit_cardInfoHtml, "deposit_cardInfo.html");
        logger.info("collectFashionAccountCardInfo方法，采集时尚版-借记卡信息，结束");
        context.put("deposit_cardInfo",deposit_cardInfoHtml);
    },
    /**
     * --------------时尚版-卡信息------------
     * 采集借记卡信息
     */
    collectFashionSubCardInfo:function (basicInfo, depositCardNum) {
        logger.info("collectFashionSubCardInfo 方法，采集时尚版-借记卡信息，开始");
        var url=urls.REQ_ACCOUNT_DEPOSIT_URL;
        var postDates=new LinkedHashMap();
        postDates.put("AcctId", basicInfo.getString("acctId"));
        postDates.put("AcctType", basicInfo.getString("acctType"));
        postDates.put("AcctCurrCode", basicInfo.getString("currCode"));
        postDates.put("AcctOofFlag", basicInfo.getString("oofFlag"));
        var depositeInfo = ajax_post(url, postDates);
        storeDataToFile(context, depositeInfo, depositCardNum+"_depositInfo.html");
        context.put(depositCardNum + "_deposit_cardInfo",depositeInfo)
    },
    /**
     * -----------------时尚版-存款信息----------
     * 采集借记卡存款信息
     */
    collectFashionDepsoitInfo:function (basicInfo,depositCardNum) {
        // var firstDateAndLastDate = commonUtils.getFirstDayAndLastDay(motheIndex);
        logger.info("collectFashionDepsoitInfo 方法，采集时尚版-借记卡存款信息，开始");
        var balanceUrl="https://perbank.abchina.com/EbankSite/AcctBalanceQryAct.do";
        var param=new LinkedHashMap();
        param.put("multiAcctsString", basicInfo.getString("acctNo"));
        var balanceJson = ajax_post(balanceUrl, param);
        storeDataToFile(context, balanceJson, depositCardNum+"_balance_json.txt");
        context.put(depositCardNum +"_balance_json",balanceJson);
        logger.info("collectFashionDepsoitInfo 方法，采集时尚版-借记卡存款信息，结束");
    },
    /**
     * ----------------时尚版-月账单数据--------
     * 采集借记卡月账单数据
     */
    collectFashionMonthBillData:function (basicInfo,dateList) {
        var downLoadFileUrl = urls.REQ_SHOPPING_DOWNLOAD_URL;
        //参数信息  acctId=卡号 acctOpenBankId=30949 acctType=401 provCode=11 acctCurCode=156 oofeFlg=0  trnStartDt=20170713  trnEndDt=20171013
        var param = new HashMap();
        param.put("acctCurCode", basicInfo.getString("currCode"));
        param.put("acctId", basicInfo.getString("acctNo"));
        param.put("acctOpenBankId", basicInfo.getString("branchNo"));
        param.put("acctType", basicInfo.getString("acctType"));
        param.put("oofeFlg", basicInfo.getString("oofFlag"));
        param.put("provCode", basicInfo.getString("provCode"));
        param.put("queryMode", "0");
        param.put("trnStartDt", dateList[0]);// 格式为20170606
        param.put("trnEndDt", dateList[1]);// 格式为20170606
        //拼接参数
        var datas=commonUtils.mapParemsEntrySet(param);
        //下载文件 返回的文件的路径 默认保存在release的temp文件夹中
        var billPaths = luaFuncs.ajaxGetFile(downLoadFileUrl, datas);
        logger.info("下载借记卡的账单数据：{}至{},下载结果为：",dateList[0],dateList[1],billPaths);
        if (billPaths==null ||billPaths.length()==0||billPaths.equals("EMPTYPATH")) {
            logger.info("当前卡{},请求下载{}至{}月账单失败，路径为空，停止抓取该月",param.get("acctId"),dateList[0],dateList[1]);
            return;
        }
        // 赋值文件到指定文件夹下
        var copyFilePath = commonUtils.filePathToLocal(context,billPaths,"借_" + "bill_" + dateList[0]+"_"+dateList[1] + "_Deposits.xls",context.get(LUA_TASK_KEY));
        logger.info("当前借记卡：{}路径为{}",param.get("acctId"),copyFilePath);
        context.put(dateList[0]+"_"+dateList[1] + "billsFilePaths",copyFilePath);
    },
    /**
     * 采集信用卡信息
     */
    collectCreditCardInfo:function (cardNum) {
        var creditCardInfoUrl = urls.REQ_CREDIT_CARD_INFO_URL;
        var param = new LinkedHashMap();
        // ccCard=6259980002580486
        param.put("ccCard", cardNum);
        var creditCardInfo = ajax_post(creditCardInfoUrl, param);
        storeDataToFile(context,creditCardInfo, cardNum+"_creditCardInfo.html");
        context.put(cardNum + "creditCardInfo",creditCardInfo)
    },
    /**
     * 采集信用卡已出账单可查询的月份
     */
    collectCreditDoneBills:function (creditIndex,fullCardNum) {
        var param = new LinkedHashMap();
        param.put("ccCardBillQry", fullCardNum);
        var initHtml = ajax_post(url, param);
        storeDataToFile(context,initHtml, creditIndex+"_queryMonth.html");
        context.put(creditIndex+"_queryMonthList",initHtml);
    },
    /**
     * 采集月账单数据
     * @param queryMonthInfo
     */
    collectCreditQueryMonthDoneBill: function (creditIndex,billDate,billMonth,creditBasicInfo) {
        // var billDate = queryMonthInfo.getString("billDate");
        // var billMonth = queryMonthInfo.getString("billMonth");
        if (billDate != null && billDate.length() > 0) {
            logger.info("抓取信用卡{}月账单", billDate);
            try {
                var param = new LinkedHashMap();
                param.put("ccCardBillQry", creditBasicInfo);
                param.put("billDate_reqry", billdate);
                param.put("ccBillQryForm_bk", "");
                Thread.sleep(1000);
                //获取账单信息，包括new_balance
                var billlDateHtml = ajax_post("https://perbank.abchina.com/EbankSite/ccBillQry2Act.do", param);
                storeDataToFile(context,billlDateHtml, creditIndex + "_credit__bills_" + billMonth + ".html");
                context.put(creditIndex + "_credit__bills_" + billMonth,billlDateHtml);
            } catch (e) {
                logger.info("抓取{}月账单失败，停止抓取，继续抓取下一个月");
                logger.info("具体的错误信息为:{}",printStackTraceToString(e));
            }
        }
    },
    /**
     * 采集账单数据明细
     * @param creditIndex
     * @param card_num
     * @param queryMonthInfo
     */
    collectCreditDoneBillInfo:function (creditIndex,card_num,billDate) {
        logger.info("抓取{}月账单失败，停止抓取，继续抓取下一个月");
        var creditDoneBillInfoUrl = urls.REQ_CREDIT_DONE_BILL_INFO_URL;
        var map = new LinkedHashMap();
        map.put("ccCardBillQry", card_num);
        map.put("billDate", billDate);
        var billDetailJson = ajax_post(creditDoneBillInfoUrl, map);
        storeDataToFile(context,billDetailJson, creditIndex + "_credit_shopping_sheets" + billDate + ".html");
        context.put(creditIndex + "_credit_shopping_sheets" + billDate,billDetailJson)

    },
    /**
     * 采集信用卡账单数据总的分期数据明细
     * @param creditIndex
     * @param card_num
     * @param queryMonthInfo
     */
    collectCreditDoneInstallmentInfo:function (creditIndex,card_num,billDate) {
        var map = new LinkedHashMap();
        map.put("ccCardBillQry", card_num);
        map.put("billDate", billDate);
        var creditDoneInstallmentInfoUrl = urls.REQ_CREDIT_DONE_INSTALLMENT_INFO_URL;
        var installmentJson = ajax_post(creditDoneInstallmentInfoUrl, map);
        storeDataToFile(context,installmentJson, creditIndex + "_credit_installment" + billDate + ".html");
        context.put(creditIndex + "_credit_installment" + billDate,installmentJson)
    },
    /**
     *  采集信用卡未出账单数据
     * @param creditIndex
     */
    collectCreditUndoneBills:function (creditIndex,card_num) {
        var creditUndoneBillURL = urls.REQ_CREDIT_UNDONE_BILLS_URL;
        var param=new LinkedHashMap();
        var parambilldate = commonUtils.getUnBillDataTime(billDate);
        param.put("ccCardBillQry", card_num);
        param.put("billDate",parambilldate.replaceAll("-", "")+billDate );
        var undoneBillInfo = ajax_post(creditUndoneBillURL, param);//现在参数不对
        context.put(creditIndex + "_creditUndoneBills",undoneBillInfo);


    },
    /*----经典版------经典版--------经典版---经典版----经典版---经典版-----经典版-----*/
    /**
     * 抓取可能的多个借记卡基本账户信息
     *
     * @return
     */
    collectSutraBasicCardInfoList:function() {
        logger.info("ABCBank:taskKey=采集借记卡集合信息");
        // ?chkAcctNo="+basicInfoStr+"&random="
        var basicHtmlCardList = ajax_get(urls.REQ_SUTRA_BASIC_INFO_LIST_URL);
        storeDataToFile(context,basicHtmlCardList,"sutra_deposit_cardlist.html");
        context.put("sutra_basic_info",basicHtmlCardList)
    },
    /**
     * 抓取可能的多个借记卡基本账户信息
     *
     * @return
     */
    collectSutraBasicCardInfo:function(cardIndex) {
        logger.info("ABCBank:taskKey=采集借记卡集合信息");
        var basicHtmlCardList = ajax_get(urls.REQ_SUTRA_BASIC_INFO_URL);
        storeDataToFile(context,basicHtmlCardList,cardIndex + "_sutra_card_info.html");
        context.put(cardIndex + "sutra_card_info",basicHtmlCardList)
    },

}